<template>
  <div>
    <operation-title
      title="图片轮播"
      :icon="TitleIcon"
    />
    <div class="divider"></div>
    <div class="func">
      <p class="attr">组件风格</p>
      <ul class="common-ul">
        <li @click="config.pageConfig.custom = '1'" :class="config.pageConfig.custom == '1' ? 'active' : ''">
          <img src="../../../../../../assets/app/other/banner/style.png" alt="" />
        </li>
        <li @click="
  config.pageConfig.custom = '2';
config.pageConfig.position = '2';
        " :class="config.pageConfig.custom == '2' ? 'active' : ''">
          <img src="../../../../../../assets/app/other/banner/style2.png" alt="" />
        </li>
        <li @click="
  config.pageConfig.custom = '3';
config.pageConfig.position = '2';
        " :class="config.pageConfig.custom == '3' ? 'active' : ''">
          <img src="../../../../../../assets/app/other/banner/style3.png" alt="" />
        </li>
      </ul>
    </div>
    <div class="divider"></div>
    <div class="func">
      <p class="attr">轮播点</p>
      <el-radio-group v-model="config.pageConfig.dotshow" class="ml-4">
        <el-radio :label="true">显示</el-radio>
        <el-radio :label="false">关闭</el-radio>
      </el-radio-group>
    </div>
    <div class="divider"></div>
    <div class="func" v-if='config.pageConfig.dotshow'>
      <p class="attr">轮播点样式</p>
      <ul class="common-ul">
        <li @click="config.pageConfig.dotStyle = '1'" :class="config.pageConfig.dotStyle == '1' ? 'active' : ''">
          <img src="../../../../../../assets/app/other/banner/dot-round.png" alt="" />
        </li>
        <li @click="config.pageConfig.dotStyle = '2'" :class="config.pageConfig.dotStyle == '2' ? 'active' : ''">
          <img src="../../../../../../assets/app/other/banner/dot-arc.png" alt="" />
        </li>
        <li @click="config.pageConfig.dotStyle = '3'" :class="config.pageConfig.dotStyle == '3' ? 'active' : ''">
          <img src="../../../../../../assets/app/other/banner/dot-square.png" alt="" />
        </li>
        <li v-if="config.pageConfig.custom == '1'" @click="config.pageConfig.dotStyle = '4'"
          :class="config.pageConfig.dotStyle == '4' ? 'active' : ''">
          <img src="../../../../../../assets/app/other/banner/dot-page.png" alt="" />
        </li>
      </ul>
    </div>
    <div class="divider"></div>
    <div class="func" v-if="config.pageConfig.dotshow && config.pageConfig.custom == '1'">
      <p class="attr">轮播点位置</p>
      <ul class="common-ul">
        <li @click="config.pageConfig.position = '1'" :class="config.pageConfig.position == '1' ? 'active' : ''">
          <img src="../../../../../../assets/app/other/banner/left.png" alt="" />
        </li>
        <li @click="config.pageConfig.position = '2'" :class="config.pageConfig.position == '2' ? 'active' : ''">
          <img src="../../../../../../assets/app/other/banner/center.png" alt="" />
        </li>
        <li @click="config.pageConfig.position = '3'" :class="config.pageConfig.position == '3' ? 'active' : ''">
          <img src="../../../../../../assets/app/other/banner/right.png" alt="" />
        </li>
      </ul>
    </div>
    <div class="divider"></div>
    <div class="func">
      <p class="attr">轮播图片</p>
      <h4>建议图片宽度750px，高度420px。</h4>
      <ul v-if="
        config.pageConfig &&
        config.pageConfig.imgList &&
        config.pageConfig.imgList.length > 0
      " class="add-item">
        <li v-for="(item, index) in config.pageConfig.imgList" :key="index">
          <div class="upload-img" v-if="item.imgUrl">
            <el-image style="width: 100%; height: 100%" :src="item.imgUrl" fit="contain"></el-image>
            <div class="upload-img_cover">
              <Upload :model-value="fileList" file-path="act/" :limit="999" :show-file-list="false"
                @success="uploadSuccess">
                <el-button @click="currentIndex = index" size="small">替换</el-button>
              </Upload>
            </div>
          </div>
          <Upload v-else v-model="item.imgUrl" file-path="act/" list-type="picture-card">
            <el-icon>
              <Plus />
            </el-icon>
          </Upload>
          <div class="link-div" @click="linkDialog(item,index)">
            <el-icon>
              <Link />
            </el-icon>
            <!-- <el-input v-model="item.url" placeholder="请填写带有https/http的链接地址" clearable></el-input> -->
            <span v-if="item.url">{{item.urlName}}</span>
            <span v-else>请选择链接</span>
            <!-- <el-icon v-if="item.name" class="close-btn"><Close /></el-icon> -->
          </div>
          <div class="move-btns">
            <el-icon
              v-show="index != 0"
              class="move-icon"
              title="上移"
              @click="moveTo('up', index)"
            >
              <Top />
            </el-icon>
            <el-icon
              v-show="index != config.pageConfig.imgList.length - 1"
              style="cursor: pointer"
              title="下移"
              @click="moveTo('down', index)"
            >
              <Bottom />
            </el-icon>
          </div>
          <el-popconfirm confirm-button-text="是" cancel-button-text="否" @confirm="sliceItem(index)" :title="
            config.pageConfig.imgList.length > 1
              ? '确定删除吗？'
              : '只有一个不可删除'
          ">
            <template #reference>
              <el-icon class="sure-close">
                <Close />
              </el-icon>
            </template>
          </el-popconfirm>
        </li>
        <li class="extra-li" @click="addItem" v-if="config.pageConfig.imgList.length < 8">
          <p>+添加{{ config.pageConfig.imgList.length }}/8</p>
        </li>
      </ul>
    </div>
    <div class="divider"></div>
    <div class="func">
      <p class="attr">样式</p>
      <ul class="common-ul">
        <li @click="config.pageConfig.style = '1'" :class="config.pageConfig.style == '1' ? 'active' : ''">
          默认
        </li>
        <li @click="config.pageConfig.style = '2'" :class="config.pageConfig.style == '2' ? 'active' : ''">
          投影
        </li>
        <li @click="config.pageConfig.style = '3'" :class="config.pageConfig.style == '3' ? 'active' : ''">
          描边
        </li>
      </ul>
      <div class="adjust-item" v-if="config.pageConfig.style == '3'">
        <el-color-picker v-model="config.pageConfig.borderColor" />
        <span>{{ config.pageConfig.borderColor }}</span>
      </div>
      <div class="adjust-item" v-if="config.pageConfig.style == '3'">
        <el-slider v-model="config.pageConfig.borderWidth" :min="0" :max="5" show-input size="small" />px
      </div>
    </div>
    <div class="divider"></div>
    <div class="func">
      <p class="attr">选择颜色</p>
      <div class="adjust-item">
        <span>底部背景</span>
        <el-color-picker v-model="config.pageConfig.color" />
        <span>{{ config.pageConfig.color }}</span>
      </div>
    </div>
    <div class="divider"></div>
    <div class="func">
      <p class="attr">边距</p>
      <div class="adjust-item">
        <span>上边距</span>
        <el-slider v-model="config.pageConfig.marginTop" :min="0" :max="50" show-input size="small" />px
      </div>
      <div class="adjust-item">
        <span>下边距</span>
        <el-slider v-model="config.pageConfig.marginBottom" :min="0" :max="50" show-input size="small" />px
      </div>
      <div class="adjust-item">
        <span>左右边距</span>
        <el-slider v-model="config.pageConfig.margin" :min="0" :max="20" show-input size="small" />px
      </div>
    </div>
    <div class="divider"></div>
    <div class="func">
      <p class="attr">圆角设置</p>
      <div class="adjust-item">
        <span>上圆角</span>
        <el-slider :min="0" :max="20" v-model="config.pageConfig.radiusTop" show-input size="small" />px
      </div>
      <div class="adjust-item">
        <span>下圆角</span>
        <el-slider :min="0" :max="20" v-model="config.pageConfig.radiusBottom" show-input size="small" />px
      </div>
    </div>
    <div class="divider"></div>
  </div>

  <href-selector
      v-model:show="linkFormVisible"
      :model-value="linkObject"
      @confirm="(link) => submitUrl(link, config.pageConfig.imgList[linkIndex])"
  /></template>

<script lang="ts">
export default {
  name: "ImgCarouselOperation",
};
</script>
<script lang="ts" setup>
import {reactive, onMounted, Ref} from "vue";
import {useHrefSelector} from "@/views/activity/actApp/components/default/compositions/useHrefSelector";
import TitleIcon from "@/assets/app/icon/bannerGoods.png";
import OperationTitle
  from "@/views/activity/actApp/components/default/operationComponents/operationTitle/OperationTitle.vue";
const config = defineProps({
  pageConfig: {
    type: Object,
    default: {
      custom: "1",
      style: "1",
      dotshow: true,
      dotStyle: "1",
      position: "1",
      color: "transparent",
      borderColor: "transparent",
      borderWidth: 1,
      marginTop: 0,
      marginBottom: 0,
      marginImg: 5,
      margin: 0,
      radiusTop: 0,
      radiusBottom: 0,
      imgList: [
        {
          imgUrl: "",
          name: "",
          url: "",
          urlType: '',
          urlName: '',
        },
        {
          imgUrl: "",
          name: "",
          url: "",
          urlType: '',
          urlName: '',
        },
      ],
    },
  },
});
// let linkIndex = 0;
const sliceItem = (idx: Number) => {
  if (config.pageConfig.imgList.length < 2) {
    return;
  } else {
    config.pageConfig.imgList.splice(idx, 1);
  }
};

const currentIndex = ref(999);
const fileList = ref([]);
// const linkFormVisible:Ref<boolean>=ref(false);
// const linkObject=ref({
//   // linkUrl:'',
//   // linkName:'',
//   // linkType:'',
//   // linkHttpUrl:''
//   url: '',
//   urlName: '',
//   urlType: '',
// });
const addItem = () => {
  if (config.pageConfig.imgList.length >= 8) {
    return;
  } else {
    config.pageConfig.imgList.push({
      imgUrl: "",
      name: "",
      url: "",
      urlType: '',
      urlName: '',
    });
  }
};
let emits = defineEmits<{ (e: "changeStyle", configStyle: Object): void }>();

const uploadSuccess = (value: any) => {
  config.pageConfig.imgList[currentIndex.value].imgUrl = value.url;
};
watch(config, (val) => {
  //过滤没有内容的图片
  const data={...config.pageConfig}
  const imgList=config.pageConfig.imgList.filter(i=>i.imgUrl||i.name||i.url||i.urlName||i.urlType)
  data.imgList=imgList
  emits("changeStyle", data);
});

const {linkFormVisible, linkIndex, linkObject, submitUrl, linkDialog} = useHrefSelector();

const moveTo=(type:'up'|'down',index:number)=>{
  const list = [...config.pageConfig.imgList]
  const item = list[index]
  //上移
  if (type == 'up') {
    const preItem = list[index - 1]
    list[index - 1] = item
    list[index] = preItem
  }
  //下移
  if (type == 'down') {
    const nextItem = list[index + 1]
    list[index] = nextItem
    list[index + 1] = item
  }
  config.pageConfig.imgList = [...list]
}
// const submitUrl=(content:any)=>{
//     config.pageConfig.imgList[linkIndex].url = content.url;
//     config.pageConfig.imgList[linkIndex].urlName = content.urlName;
//     config.pageConfig.imgList[linkIndex].urlType = content.urlType;
//     linkFormVisible.value = false
// };
// const onClose=()=>{
//   linkFormVisible.value = false
// };
// const linkDialog = (item:any,idx: number) => {
//   linkIndex = idx;
//   // if(item.urlType=='link'){
//   //   linkObject.value.linkHttpUrl=item.url;
//   //   linkObject.value.linkUrl = '';
//   //   linkObject.value.linkName= item.urlName;
//   //   linkObject.value.linkType= item.urlType;
//   // }else{
//   //   linkObject.value.linkHttpUrl='';
//   //   linkObject.value.linkUrl = item.url;
//   //   linkObject.value.linkName= item.urlName;
//   //   linkObject.value.linkType= item.urlType;
//   // }
//   linkObject.value.urlName = item.urlName;
//   linkObject.value.url = item.url;
//   linkObject.value.urlType = item.urlType;
//   linkFormVisible.value = true;
// };
</script>

<style lang="scss" scoped>
.title {
  padding: 8px 23px;
  overflow: hidden;
  border-bottom: 5px solid #f6f7f9;
  display: flex;
  position: relative;
  height: 50px;
  box-sizing: border-box;

  img {
    position: absolute;
    left: 0;
    top: -5px;
  }

  h5 {
    font-size: 16px;
    font-weight: 700;
    position: absolute;
    left: 70px;
    top: 0;
    line-height: 50px;
  }
}

.func {
  border-bottom: 5px solid #f6f7f9;
  padding: 20px 16px;
  font-size: 16px;

  .attr {
    line-height: 1;
    color: #47565d;
    font-weight: bolder;
    margin-bottom: 20px;
  }

  ul.common-ul {
    list-style: none;
    display: flex;
    align-items: center;
    justify-content: center;

    li {
      flex: 1;
      height: 30px;
      display: flex;
      align-items: center;
      justify-content: center;
      border-radius: 4px 0 0 4px;
      border: 2px solid #ededed;
      border-collapse: collapse;

      &.active {
        border: 2px solid #fb6638;
      }

      img {
        max-width: 100% !important;
        max-height: 100% !important;
      }
    }
  }

  h4 {
    position: relative;
    padding: 8px 16px 8px 16px;
    border-radius: 6px;
    color: red;
    font-size: 16px;
    line-height: 22px;
    margin-bottom: 10px;
    border: 1px solid #d8e8fc;
    background-color: #e5f0fe;
  }

  div.adjust-item {
    display: flex;
    align-items: center;
    margin-top: 3px;
    margin-bottom: 10px;

    span {
      width: 64px;
      text-align: right;
      margin-right: 15px;
      color: #666;
    }

    :deep(.el-slider) {
      width: 240px;
      margin-right: 3px;
      margin-left: 15px;
    }

    :deep(.el-slider__runway.show-input) {
      margin-right: 15px;
    }

    :deep(.el-input-number) {
      width: 100px;
      background: #f4f3f7;
      border: 1px solid #f4f3f7;
    }
  }

  .add-item {
    width: 100%;

    li {
      background: #f4f3f7;
      border-radius: 4px;
      padding: 22px 20px 10px 10px;
      color: #999;
      margin-top: 10px;
      margin-bottom: 20px;
      position: relative;
      display: flex;
      // align-items: center;

      &.extra-li {
        justify-content: center;
      }

      .upload-img {
        width: 50px;
        height: 50px;
        line-height: 50px;
        margin-right: 10px;
        border: 1px dashed #ededed;
        text-align: center;
        color: #dad9d9;
        background-size: 100% 100%;
        background-repeat: no-repeat;
        float: left;
        position: relative;

        :deep(.el-image) {
          width: 50px;
          height: 50px;
        }

        &:hover {
          .upload-img_cover {
            opacity: 1;
          }
        }

        .upload-img_cover {
          opacity: 0;
          position: absolute;
          transition: all 0.3s ease;
          top: 0;
          left: 0;
          width: 100%;
          height: 100%;
          background-color: rgba(0, 0, 0, 0.7);
          display: flex;
          flex-direction: column;
          align-items: center;
          justify-content: space-evenly;
        }
      }

      :deep(.el-upload-list) {
        width: 50px;
        height: 50px;
        margin-right: 10px;
      }

      :deep(.el-upload--picture-card) {
        width: 50px;
        height: 50px;
      }

      .link-div {
        width: calc(100% - 80px);
        display: flex;
        align-items: center;
        background: #e8effc;
        border: 0;
        padding-left: 16px;
        cursor: pointer;
        position: relative;

        :deep(.el-icon) {
          margin-right: 10px;
        }

        :deep(.el-input) {
          font-size: 12px;
          height: 30px;
          width: calc(100% - 30px);
        }

        .close-btn {
          position: absolute;
          right: 5px;
        }
      }

      .sure-close {
        position: absolute;
        right: 5px;
        top: 5px;
        cursor: pointer;
      }
    }
  }
}
.move-btns {
  position: absolute;
  top: 5px;
  right: 24px;
  .move-icon {
    margin-right: 4px;
    cursor: pointer;
  }
}

:deep(.el-radio__label) {
  font-size: 16px;
}
</style>
